package com.zlx.mall.basic.web.holder;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.zlx.mall.basic.web.filter.ContextHolderListener;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

import javax.servlet.http.Cookie;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qingke
 * @see org.springframework.web.context.request.RequestContextHolder
 **/
public class RequestContextHolder {

    public static final TransmittableThreadLocal<HttpHeaders> HEADERS = new TransmittableThreadLocal<>();
    public static final TransmittableThreadLocal<URI> URI = new TransmittableThreadLocal<>();
    public static final TransmittableThreadLocal<MultiValueMap<String, String>> URI_VARS = new TransmittableThreadLocal<>();
    public static final TransmittableThreadLocal<String> REMOTE_ADDRESS = new TransmittableThreadLocal<>();
    public static final TransmittableThreadLocal<Map<String, String>> COOKIES = new TransmittableThreadLocal<>();
    public static final TransmittableThreadLocal<String> CONTENT = new TransmittableThreadLocal<>();

    static {
        ContextHolderListener.register(HEADERS);
        ContextHolderListener.register(URI);
        ContextHolderListener.register(URI_VARS);
        ContextHolderListener.register(REMOTE_ADDRESS);
        ContextHolderListener.register(COOKIES);
        ContextHolderListener.register(CONTENT);
    }

    public static HttpHeaders getHeaders() {
        return HEADERS.get();
    }

    public static void setHeaders(HttpHeaders value) {
        HEADERS.set(value);
    }

    public static String getHeader(String key) {
        return getHeaders().getFirst(key);
    }

    public static URI getUri() {
        return URI.get();
    }

    public static void setUri(URI value) {
        URI.set(value);
    }

    public static MultiValueMap<String, String> getUriVars() {
        return URI_VARS.get();
    }

    public static void setUriVars(MultiValueMap<String, String> value) {
        URI_VARS.set(value);
    }

    public static String getUriVar(String key) {
        return getUriVars().getFirst(key);
    }

    public static String getRemoteAddress() {
        return REMOTE_ADDRESS.get();
    }

    public static void setRemoteAddress(String value) {
        REMOTE_ADDRESS.set(value);
    }

    public static void setCookies(Cookie[] cookies) {
        if (cookies != null && cookies.length > 0) {
            Map<String, String> map = new HashMap<>(16);
            for (Cookie cookie : cookies) {
                map.put(cookie.getName(), cookie.getValue());
            }
            COOKIES.set(map);
        }
    }

    public static Map<String, String> getCookies() {
        return COOKIES.get();
    }

    public static String getCookieValue(String name) {
        Map<String, String> cookies = COOKIES.get();
        if (cookies != null && !cookies.isEmpty()) {
            return cookies.get(name);
        }
        return null;
    }

    public static String getContent() {
        return CONTENT.get();
    }

    public static void setContent(String value) {
        CONTENT.set(value);
    }

    public static void remove() {
        HEADERS.remove();
        URI.remove();
        URI_VARS.remove();
        REMOTE_ADDRESS.remove();
        COOKIES.remove();
        CONTENT.remove();
    }
}
